package com.yfproject.aspect;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
public class HttpAspect {
	
	private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);
	
	@Pointcut("execution(public * com.yfproject.controller.LableController.*(..))")
	public void log() {
		
	}
	
	@Pointcut("execution(public * com.yfproject.controller.LableController2.*(..))")
	public void log2() {
		
	}
	
	@Pointcut("execution(public * com.yfproject.controller.LableController3.*(..))")
	public void log3() {
		
	}
	
	@Before("log()")
	public void doBefore(JoinPoint joinPoint) {
		ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		
		logger.info("aop aspect before");
		
		//url
		logger.info("url={}",request.getRequestURI());
		
		//method
		logger.info("method={}",request.getMethod());
		
		//ip
		logger.info("ip={}",request.getRemoteAddr());
		
		//类方法
		logger.info("class_method={}",
					joinPoint.getSignature().getDeclaringTypeName()+"."
					+joinPoint.getSignature().getName());
		
		//参数
		String args = new String();
		int i = 0;
		for(;i<joinPoint.getArgs().length-1;i++) {
			args = args + joinPoint.getArgs()[i] + ",";
			//logger.info("args{}={}",i,joinPoint.getArgs()[i]);
		}
		args = args + joinPoint.getArgs()[i];
		logger.info("args={}",args);
	}
	
	@Before("log2()")
	public void doBefore2(JoinPoint joinPoint) {
		ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		
		logger.info("aop aspect before");
		
		//url
		logger.info("url={}",request.getRequestURI());
		
		//method
		logger.info("method={}",request.getMethod());
		
		//ip
		logger.info("ip={}",request.getRemoteAddr());
		
		//类方法
		logger.info("class_method={}",
					joinPoint.getSignature().getDeclaringTypeName()+"."
					+joinPoint.getSignature().getName());
		
		//参数
		logger.info("args=null");
	}
	
	@Before("log3()")
	public void doBefore3(JoinPoint joinPoint) {
		ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		
		logger.info("aop aspect before");
		
		//url
		logger.info("url={}",request.getRequestURI());
		
		//method
		logger.info("method={}",request.getMethod());
		
		//ip
		logger.info("ip={}",request.getRemoteAddr());
		
		//类方法
		logger.info("class_method={}",
					joinPoint.getSignature().getDeclaringTypeName()+"."
					+joinPoint.getSignature().getName());
		
		//参数
		String args = new String();
		int i = 0;
		for(;i<joinPoint.getArgs().length-1;i++) {
			args = args + joinPoint.getArgs()[i] + ",";
			//logger.info("args{}={}",i,joinPoint.getArgs()[i]);
		}
		args = args + joinPoint.getArgs()[i];
		logger.info("args={}",args);
	}
	
	@After("log()")
	public void doAfter() {
		logger.info("aop aspect after");
	}
	
	@After("log2()")
	public void doAfter2() {
		logger.info("aop aspect after");
	}
	
	@After("log3()")
	public void doAfter3() {
		logger.info("aop aspect after");
	}
	
	@AfterReturning(returning="object",pointcut="log()")
	public void doAfterRunning(Object object) {
		logger.info("response={}",object.toString());
		logger.info("aop aspect @AfterReturning");
	}
	
	@AfterReturning(returning="object",pointcut="log2()")
	public void doAfterRunning2(Object object) {
		logger.info("response=null");
		logger.info("aop aspect @AfterReturning");
	}
	
	@AfterReturning(returning="object",pointcut="log3()")
	public void doAfterRunning3(Object object) {
		logger.info("response=null");
		logger.info("aop aspect @AfterReturning");
	}
//	@Before("execution(public * com.yfproject.controller.LableController.getLabelInfoById(..))")
//	public void doBefore() {
//		System.out.println("aop aspect before");
//	}
//	
//	@After("execution(public * com.yfproject.controller.LableController.getLabelInfoById(..))")
//	public void doAfter() {
//		System.out.println("aop aspect after");
//	}

}
